package org.liaohailong.victorlibrary.ui;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;

import org.jetbrains.annotations.Nullable;
import org.liaohailong.library.widget.ptr.PullToRefreshLayout;
import org.liaohailong.victorlibrary.R;
import org.liaohailong.victorlibrary.base.BaseActivity;

import java.util.LinkedList;
import java.util.List;

/**
 * Author: liaohailong
 * Date: 2019/5/9
 * Time: 20:55
 * Description: 下拉刷新控件演示
 **/
public class PtrActivity extends BaseActivity {

    public static void show(Context context) {
        Intent intent = new Intent(context, PtrActivity.class);
        context.startActivity(intent);
    }

    private RecyclerView recyclerView;
    private PullToRefreshLayout ptrLayout;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ptr);

        recyclerView = findViewById(R.id.recycler_view);
        ptrLayout = findViewById(R.id.ptr_layout);

//        recyclerView.setLayoutManager(new LinearLayoutManager(this));
//        recyclerView.setLayoutManager(new LearnLayoutManager(this));
//        recyclerView.setLayoutManager(new CascadeFlowLayoutManager(this, 3));
        recyclerView.setLayoutManager(new StaggeredGridLayoutManager(3, RecyclerView.VERTICAL));
        final Adapter adapter = new Adapter();
        recyclerView.setAdapter(adapter);
        makeData(30, adapter);


        ptrLayout.setOnPullToRefreshCallback(new PullToRefreshLayout.OnPullToRefreshCallback() {
            @Override
            public void onRefreshBegin() {
                makeData(5, adapter);
                recyclerView.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        ptrLayout.stopRefresh();
                    }
                }, 2000);
            }

            @Override
            public void onRefreshCancel() {

            }
        });
    }

    private void makeData(int num, Adapter adapter) {
        List<String> data = new LinkedList<>();
        for (int i = 0; i < num; i++) {
            data.add(String.valueOf(i));
        }
        adapter.addData(data);
    }


    private class Adapter extends RecyclerView.Adapter<ViewHolder> {
        private List<String> data = new LinkedList<>();
        private int[] colors = {
                Color.DKGRAY,
                Color.GRAY,
                Color.LTGRAY,
                Color.WHITE,
                Color.RED,
                Color.GREEN,
                Color.BLUE,
                Color.YELLOW,
                Color.CYAN
        };

        private final int[] size = {200, 400, 900};


        void addData(List<String> dataList) {
            int position = data.size();
            int count = dataList.size();
            data.addAll(dataList);
            notifyItemRangeInserted(position, count);
        }

        @NonNull
        @Override
        public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_textview, parent, false);
            RecyclerView.LayoutParams layoutParams = (RecyclerView.LayoutParams) view.getLayoutParams();
//            int dim56 = getResources().getDimensionPixelSize(R.dimen.dim56);
//            int dim1920 = getResources().getDimensionPixelSize(R.dimen.dim1920);
//            layoutParams.width = dim1920 / 3;
            layoutParams.height = viewType;
            view.setLayoutParams(layoutParams);
            return new ViewHolder(view);
        }

        @Override
        public int getItemViewType(int position) {
            return size[position % size.length];
        }

        @Override
        public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
            TextView textView = holder.itemView.findViewById(R.id.tv_title);
            ImageView imageView = holder.itemView.findViewById(R.id.iv_avatar);
            textView.setText(String.valueOf(position));
            imageView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    recyclerView.scrollToPosition(0);
                    ptrLayout.autoRefresh();
                }
            });

//            int color = colors[position % colors.length];
//            holder.itemView.setBackgroundColor(color);

        }

        @Override
        public int getItemCount() {
            return data.size();
        }
    }

    private class ViewHolder extends RecyclerView.ViewHolder {

        private ViewHolder(@NonNull View itemView) {
            super(itemView);
        }
    }
}
